const express = require('express');
const { v4: uuidv4 } = require('uuid');
const bodyParser = require('body-parser');
const { pick } = require('lodash');
const { getAvatar } = require('./utils');
const app = express();
const port = 3000;

const users = [
  {
    id: '0',
    name: 'Yeaon',
    age: 18,
    gender: 1, // 0 男 1女
    avatar: 'https://picsum.photos/150/150?random=1',
  },
  {
    id: '1',
    name: 'Wang',
    age: 20,
    gender: 0, // 0 男 1女
    avatar: 'https://picsum.photos/200/200?random=2',
  },
  {
    id: '2',
    name: 'Li',
    age: 22,
    gender: 1, // 0 男 1女
    avatar: 'https://picsum.photos/200/300?random=3',
  },
];

const comments = [
  {
    userID: '0',
    comment: '这是第一条评论',
    created: new Date(),
    articleID: 78,
    upStar: 0,
    downStar: 0,
    commentID: uuidv4(),
    children: [],
  },
  {
    userID: '1',
    comment: '这是第二条评论',
    created: new Date(),
    articleID: 78,
    upStar: 0,
    downStar: 0,
    commentID: uuidv4(),
    children: [],
  },
  {
    userID: '2',
    comment: '这是第三条评论',
    created: new Date(),
    articleID: 78,
    upStar: 0,
    downStar: 0,
    commentID: uuidv4(),
    children: [],
  },
];

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.get('/test', (req, res) => {
  res.send('hello world!');
});

app.get('/user', (req, res) => {
  const { id } = req.query;
  const user = users.find((u) => u.id === id);
  res.send({
    status: 200,
    success: true,
    data: user,
  });
  return;
});

// 发布评论
app.post('/addComment', (req, res) => {
  const data = {
    ...req.body,
    created: new Date(),
    chidlren: [],
  };
  comments.push(data);
  res.send({
    status: 200,
    message: true,
    data: null,
  });
});

// 回复评论
app.post('/replyComment/:commentID', (req, res) => {
  // console.log(req, 'req');
  const pCommentID = req.params.commentID;
  const data = Object.assign(
    pick(req.body, ['userID', 'comment', 'articleID', 'upStart', 'downStar']),
    {
      commentID: uuidv4(),
      created: new Date(),
      pCommentID: pCommentID,
      children: [],
    },
  );

  const pComment = comments.find((c) => c.commentID === pCommentID);
  if (!pComment?.children) {
    pComment.children = [data];
  } else {
    pComment.children.push(data);
  }
  res.send({
    status: 200,
    message: true,
    data: null,
  });
  console.log(comments[0].children, 'comments');
  return;
});

app.get('/getComment', (req, res) => {
  const { id } = req.query;
  if (!id) {
    const data = getAvatar(comments, users);
    res.send({
      status: 200,
      message: true,
      data,
    });
  } else {
    const data = comments.filter((com) => com.userID === id);
    data.forEach((item) => {
      const { avatar, name } = users.find((u) => u.id === item.userID);
      return {
        ...item,
        avatar,
        name,
      };
    });
  }
});

app.put('/onSupport', (req, res) => {
  console.log(req.body);
  const { type, pCommentID, commentID } = req.body;
  let comment;
  if (pCommentID === undefined) {
    comment = comments.find((c) => c.commentID === commentID);
  } else {
    pComment = comments.find((c) => c.commentID === pCommentID);
    comment = pCommentID.find((v) => v.commentID === commentID);
  }
  console.log(type, 'type');
  if (type === 1) {
    comment.upStar += 1;
  } else {
    comment.downStar += 1;
  }
  res.send({
    status: 200,
    message: true,
    data: null,
  });
});

app.listen(port, () => {
  console.log('Hi, Now port 3000 is listening...');
});
